package com.zt.rongda.reimburse.listener;

import java.util.Date;

import com.zt.bpm.service.FormFactory;
import com.zt.rongda.constant.BusConstant;
import com.zt.rongda.reimburse.inter.ReimburseBillMapper;
import com.zt.rongda.reimburse.model.ReimburseBill;
import com.zt.shiro.GlobalInput;
import com.zt.system.service.SpringServiceFactory;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.runtime.ProcessInstance;

public class AuditListener implements TaskListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4245799204542353538L;

	@Override
	public void notify(DelegateTask delegateTask){
		String processInstanceId = delegateTask.getProcessInstanceId();
		ProcessInstance processInstance = delegateTask.getExecution().getEngineServices()
				.getRuntimeService().createProcessInstanceQuery()
				.processInstanceId(processInstanceId).singleResult();
		String businessKey = processInstance.getBusinessKey();

		ReimburseBillMapper reimburseBillMapper = (ReimburseBillMapper) SpringServiceFactory.getBean("reimburseBillMapper");
		
		String reimburseStatus = (String)delegateTask.getVariable("auditComment");
		String auditRemark = (String)delegateTask.getVariable("auditRemark");
		ReimburseBill reimburseBill = new ReimburseBill();
		reimburseBill.setReimburseId(FormFactory.getBusinessId(businessKey));
		reimburseBill.setModifyTime(new Date());
		if(BusConstant.AUDIT_TRUE.equals(reimburseStatus)) {
			reimburseBill.setStatus(BusConstant.ReimburseBill.STATUS_CHECKED_TRUE);
		} else {
			reimburseBill.setStatus(BusConstant.ReimburseBill.STATUS_CHECKED_FALSE);
		}
		reimburseBill.setRemark(auditRemark);
		reimburseBill.setOperator(GlobalInput.getCurrentUserId());
		reimburseBillMapper.updateByPrimaryKeySelective(reimburseBill);

	}

}
